package jp.co.hcs.attendancemanagement.inputtableb.logic;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import jp.co.hcs.attendancemanagement.AMConstants;
import jp.co.hcs.attendancemanagement.StrutsMessageConstants;
import jp.co.hcs.attendancemanagement.common.ApprovalRecordData;
import jp.co.hcs.attendancemanagement.common.ApprovalRecordRegister;
import jp.co.hcs.attendancemanagement.data.dao.EffortActualDAO;
import jp.co.hcs.attendancemanagement.data.dao.EmployeeMasterDAO;
import jp.co.hcs.attendancemanagement.data.dao.HolidayDAO;
import jp.co.hcs.attendancemanagement.data.dao.WorkingTimeActualDAO;
import jp.co.hcs.attendancemanagement.data.entity.EffortActual;
import jp.co.hcs.attendancemanagement.data.entity.ex.EffortActualExample;
import jp.co.hcs.attendancemanagement.data.entity.ex.EffortActualParameter;
import jp.co.hcs.attendancemanagement.exception.AMException;
import jp.co.hcs.attendancemanagement.exception.AMSQLException;
import jp.co.hcs.attendancemanagement.exception.AMSystemException;
import jp.co.hcs.attendancemanagement.inputtableb.logic.dto.EmployerInfoDTO;
import jp.co.hcs.attendancemanagement.inputtableb.logic.dto.ProjectEffortInfoDTO;
import jp.co.hcs.attendancemanagement.inputtableb.logic.dto.WorkDayDTO;
import jp.co.hcs.attendancemanagement.logic.LogicBase;
import jp.co.hcs.attendancemanagement.util.StringUtil;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.util.MessageResources;

/**
 * The Class InputTableBLogic.
 */
public class InputTableBLogic extends LogicBase {

  /** The input table b logic. */
  private static InputTableBLogic inputTableBLogic = new InputTableBLogic();

  /** The msg resource. */
  private MessageResources msgResource = MessageResources
      .getMessageResources("MessageResourcesForStruts");

  /**
   * Gets the single instance of InputTableBLogic.
   * 
   * @return single instance of InputTableBLogic
   */
  public static InputTableBLogic getInstance() {
    return inputTableBLogic;
  }

  /**
   * Gets the employer info.
   * 
   * @param employerCode the employer code
   * @return the employer info
   * @throws AMException the aM exception
   */
  public EmployerInfoDTO getEmployerInfo(String employerCode) throws AMException {
    EmployerInfoDTO employerInfoDTO = null;

    try {
      EmployeeMasterDAO employeeMasterDAO = daoFactory.getEmployeeMasterDAO();
      employerInfoDTO = employeeMasterDAO.getEmployerInfo(employerCode);
    } catch (SQLException e) {
      throw new AMSQLException(msgResource.getMessage("error.F0103.error"));
    }
    if (employerInfoDTO == null) {
      throw new AMSystemException(msgResource.getMessage("error.F0107.error"));
    }
    return employerInfoDTO;
  }

  // Get project effort list from Database
  /**
   * Gets the project effort info list.
   * 
   * @param effortActualParameter the effort actual parameter
   * @return the project effort info list
   * @throws AMException the aM exception
   */
  public List<ProjectEffortInfoDTO> getProjectEffortInfoList(
      EffortActualParameter effortActualParameter) throws AMException {
    EffortActualDAO effortActualDAO = daoFactory.getEffortActualDAO();
    List<ProjectEffortInfoDTO> projectEffortInfoDTOList = new ArrayList<ProjectEffortInfoDTO>();
    try {
      projectEffortInfoDTOList = effortActualDAO.getListProjectEffortInfoDTO(effortActualParameter);
      if (projectEffortInfoDTOList != null && projectEffortInfoDTOList.size() != 0) {
        for (ProjectEffortInfoDTO p : projectEffortInfoDTOList) {
          p.setSeriByProjectNumber();
          p.setOldSeri(p.getSeri());
          p.setOldProjectCode(p.getProjectCode());
          p.setOldWorkCode(p.getWorkCode());
        }
      }
    } catch (SQLException e) {
      throw new AMSQLException(msgResource.getMessage("error.F0103.error"));
    }

    return projectEffortInfoDTOList;
  }

  // Delete Project effort list in database
  /**
   * Detete project effort info list.
   * 
   * @param projectEffortInfoDTOList the project effort info dto list
   * @return true, if successful
   * @throws AMException the aM exception
   */
  public boolean deteteProjectEffortInfoList(List<ProjectEffortInfoDTO> projectEffortInfoDTOList)
      throws AMException {
    if (projectEffortInfoDTOList != null && projectEffortInfoDTOList.size() != 0) {
      Iterator<ProjectEffortInfoDTO> iterator = projectEffortInfoDTOList.iterator();
      ProjectEffortInfoDTO projectEffortInfoDTOtemp;
      try {
    	  daoFactory.startTransaction();
	      while (iterator.hasNext()) {
	        projectEffortInfoDTOtemp = iterator.next();
	        if (projectEffortInfoDTOtemp.getSelect() != null && projectEffortInfoDTOtemp.getSelect()) {
	          EffortActualDAO effortActualDAO = daoFactory.getEffortActualDAO();
	            effortActualDAO.deleteEffortActualInMonth(projectEffortInfoDTOtemp.getEmployerCode(),
	                projectEffortInfoDTOtemp.getWorkCode(), projectEffortInfoDTOtemp
	                    .getActualEffortYearMonth(), projectEffortInfoDTOtemp.getProjectCode());
	          
	          iterator.remove();
	        }
	      }
	      daoFactory.commitTransaction();
      } catch (SQLException e) {
          throw new AMSQLException(msgResource.getMessage("error.F0103.error"));
      }
      finally{
    	  try{
    		  daoFactory.endTransaction();
    	  }catch (SQLException ex) {
    		  throw new AMSQLException(msgResource.getMessage("error.F0103.error"));
		}
      }
    }
    return true;
  }

  // save Project effort info to database follow logic to save.
  /**
   * Save project effort info list.
   * 
   * @param projectEffortInfoDTOList the project effort info dto list
   * @param effortActualParameter the effort actual parameter
   * @return the action messages
   * @throws AMException the aM exception
   */
  public ActionMessages saveProjectEffortInfoList(
      List<ProjectEffortInfoDTO> projectEffortInfoDTOList,
      EffortActualParameter effortActualParameter) throws AMException {
    ActionMessages errors = new ActionMessages();
    EffortActualDAO effortActualDAO = daoFactory.getEffortActualDAO();
    try {
    	daoFactory.startTransaction();
      for (ProjectEffortInfoDTO p : projectEffortInfoDTOList) {
        // in case: change project code and work code
        if (!p.getOldProjectCode().equals(p.getProjectCode())
            || !p.getOldWorkCode().equals(p.getWorkCode()) || !p.getOldSeri().equals(p.getSeri())) {
          if (!p.isbAdd()) {
            // update database with condition old project code and old work code
            EffortActual effortActual = new EffortActual();
            effortActual.setProjectCode(p.getProjectCode());
            effortActual.setProjectId(p.getProjectCode()+p.getSeri());
            effortActual.setWorkingId(p.getWorkCode());

            EffortActualExample example = new EffortActualExample();
            jp.co.hcs.attendancemanagement.data.entity.ex.EffortActualExample.Criteria criteria =
                example.createCriteria();
            criteria.andActualEffortYearMonthEqualTo(p.getActualEffortYearMonth());
            criteria.andEmployeeIdEqualTo(p.getEmployerCode());
            criteria.andProjectCodeEqualTo(p.getOldProjectCode());
            criteria.andWorkingIdEqualTo(p.getOldWorkCode());
            effortActualDAO.updateEffortActualByExampleSelective(effortActual, example);

            p.setOldProjectCode(p.getProjectCode());
            p.setOldWorkCode(p.getWorkCode());
            p.setOldSeri(p.getSeri());
          }
        }
        EffortActualExample example;
        jp.co.hcs.attendancemanagement.data.entity.ex.EffortActualExample.Criteria criteria;
        // in case: work time change in specific day, delete this record and add
        // new record with changed
        // detete in database
        for (WorkDayDTO w : p.getWorkDayDTOList()) {
          if (w.isbChanged()) {
            // delete
            example = new EffortActualExample();
            criteria = example.createCriteria();
            criteria.andActualEffortYearMonthEqualTo(p.getActualEffortYearMonth());
            criteria.andEmployeeIdEqualTo(p.getEmployerCode());
            criteria.andProjectCodeEqualTo(p.getProjectCode());
            criteria.andWorkingIdEqualTo(p.getWorkCode());
            criteria.andActualEffortDayEqualTo(w.getDayNumber());
            effortActualDAO.deleteEffortActualByExample(example);
          }
        }// end for w
      }// end for p

      // initialize common tool
      ApprovalRecordRegister approvalRegister = new ApprovalRecordRegister();
      int resultCode;
      
      for (ProjectEffortInfoDTO p : projectEffortInfoDTOList) {
        // set effort regist flag OFF
        ApprovalRecordData approvalData = new ApprovalRecordData();
        approvalData.setEmployeeId(effortActualParameter.getEmployerCode());
        approvalData.setApprovalTypeId(AMConstants.APPROVAL_TYPE_DAILY);
        approvalData.setTargetYearMonth(effortActualParameter.getSelectPeriod());
        approvalData.setTargetDay(effortActualParameter.getEffortActualDate());
        approvalData.setTimingRegistCode(0);
        approvalData.setDayRegistCode(0);
        approvalData.setEffortRegistCode(2);
        approvalData.setApplicationNo(StringUtil.EMPTY);
        approvalData.setProgramId(AMConstants.PGM_5041_SCREEN_ID);
        resultCode = approvalRegister.registerApproval(approvalData);
        
        if (resultCode == AMConstants.ERROR_FINISH) {
            // rollback
            errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage(StrutsMessageConstants.ALARM_W1068_ERROR));
            return errors;
        }
          
        if (p.isbAdd()) {
          p.setbAdd(false);
        }
        EffortActual effortActual;
        // in case: work time change in specific day, delete this record and add
        // new record with changed
        EffortActualParameter effortActualParameterTemp;
        for (WorkDayDTO w : p.getWorkDayDTOList()) {
          if (w.isbChanged()) {
        	String workingTime = w.getWorkTime(); 
        	if(workingTime == null || workingTime.length() == 0){
        		break;
        	}
            // add new with change value work time
            effortActual = new EffortActual();
            effortActual.setEmployeeId(p.getEmployerCode());
            effortActual.setActualEffortYearMonth(p.getActualEffortYearMonth());
            effortActual.setActualEffortDay(w.getDayNumber());

            effortActualParameterTemp = new EffortActualParameter();
            effortActualParameterTemp.setEmployerCode(p.getEmployerCode());
            effortActualParameterTemp.setSelectPeriod(p.getActualEffortYearMonth());
            effortActualParameterTemp.setEffortActualDate(w.getDayNumber());

            int maxSEQ = effortActualDAO.getMaxSEQNumber(effortActualParameterTemp);

            effortActual.setSeqNo((short) (maxSEQ + 1));
            effortActual.setRegisterId(p.getEmployerCode());
            effortActual.setRegisterDate(new Date());
            effortActual.setRegisterTime(new Date());
            effortActual.setProjectId(p.getProjectCode() + p.getSeri());
            effortActual.setProjectCode(p.getProjectCode());
            effortActual.setWorkingId(p.getWorkCode());

            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.AM_PM, Calendar.AM);
            cal.set(Calendar.HOUR, Integer.parseInt(workingTime.substring(0, 2)));
            cal.set(Calendar.MINUTE, Integer.parseInt(workingTime.substring(
                workingTime.length() - 2, workingTime.length())));
            cal.set(Calendar.SECOND, 0);
            effortActual.setWorkingTime(cal.getTime());
            effortActualDAO.insertEffortActualSelective(effortActual);
            w.setOldWorkTime(w.getWorkTime());
          }
        }// end for w
        
        // set effort regist flag ON
        approvalData = new ApprovalRecordData();
        approvalData.setEmployeeId(effortActualParameter.getEmployerCode());
        approvalData.setApprovalTypeId(AMConstants.APPROVAL_TYPE_DAILY);
        approvalData.setTargetYearMonth(effortActualParameter.getSelectPeriod());
        approvalData.setTargetDay(effortActualParameter.getEffortActualDate());
        approvalData.setTimingRegistCode(0);
        approvalData.setDayRegistCode(0);
        approvalData.setEffortRegistCode(1);
        approvalData.setApplicationNo(StringUtil.EMPTY);
        approvalData.setProgramId(AMConstants.PGM_5041_SCREEN_ID);
        resultCode = approvalRegister.registerApproval(approvalData);
        
        if (resultCode == AMConstants.ERROR_FINISH) {
          // rollback
          errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage(StrutsMessageConstants.ALARM_W1068_ERROR));
          return errors;
        }
      }// end for p
     
      // commit data
      daoFactory.commitTransaction();
      
    } catch (SQLException e) {
      throw new AMSQLException(msgResource.getMessage("error.F0103.error"));
    }
    finally{
      try {
        daoFactory.endTransaction();
      } catch (SQLException e) {
        throw new AMSQLException(msgResource.getMessage("error.F0103.error"));
      }
    }

    return errors;
  }

  // set change value flag for all days of month
  /**
   * Sets the value change flags.
   * 
   * @param projectEffortInfoDTOList the new value change flags
   */
  public void setValueChangeFlags(List<ProjectEffortInfoDTO> projectEffortInfoDTOList) {
    for (ProjectEffortInfoDTO p : projectEffortInfoDTOList) {
      for (WorkDayDTO w : p.getWorkDayDTOList()) {
        if (w.getOldWorkTime().length() == 0 && w.getWorkTime().length() > 0) {
          w.setbChanged(true);
        } else if (w.getOldWorkTime().length() > 0 && w.getWorkTime().length() >= 0
            && !w.getOldWorkTime().equals(w.getWorkTime())) {
          w.setbChanged(true);
        } else {
          w.setbChanged(false);
        }
      }
    }
  }

  // set Project Change flag for all project list
  /**
   * Sets the project change flag.
   * 
   * @param projectEffortInfoDTOList the new project change flag
   */
  public void setProjectChangeFlag(List<ProjectEffortInfoDTO> projectEffortInfoDTOList) {
    for (ProjectEffortInfoDTO p : projectEffortInfoDTOList) {
      if (!p.getOldProjectCode().equals(p.getProjectCode())
          || !p.getOldWorkCode().equals(p.getWorkCode()) || !p.getOldSeri().equals(p.getSeri())) {
        p.setbChanged(true);
      } else {
        p.setbChanged(false);
      }
    }
  }

  // init data for new Project Effort Info and add it to Project Effort Info
  // List
  /**
   * Inits the add project effort info.
   * 
   * @param projectEffortInfoDTOList the project effort info dto list
   * @param dayOfMonth the day of month
   * @param effortActualParameter the effort actual parameter
   * @param employerCode the employer code
   * @throws AMException the aM exception
   */
  public void initAddProjectEffortInfo(List<ProjectEffortInfoDTO> projectEffortInfoDTOList,
      int dayOfMonth, EffortActualParameter effortActualParameter, String employerCode)
      throws AMException {
    if (projectEffortInfoDTOList != null) {
      HolidayDAO holidayDAO = daoFactory.getHolidayDAO();
      String selectPeriod = effortActualParameter.getSelectPeriod();
      int inputYear = Integer.parseInt(selectPeriod.substring(0, selectPeriod.length() - 2));
      int inputMonth =
          Integer
              .parseInt(selectPeriod.substring(selectPeriod.length() - 2, selectPeriod.length()));
      List<WorkDayDTO> workDayDTOList =
          createWorkDayDTOList(holidayDAO, selectPeriod, dayOfMonth, inputYear, inputMonth);
      ProjectEffortInfoDTO projectEffortInfoDTONew = new ProjectEffortInfoDTO();
      projectEffortInfoDTONew.setbAdd(true);
      projectEffortInfoDTONew.setEmployerCode(employerCode);
      projectEffortInfoDTONew.setWorkDayDTOList(workDayDTOList);
      projectEffortInfoDTONew.setProjectCode(StringUtil.EMPTY);
      projectEffortInfoDTONew.setProjectName(StringUtil.EMPTY);
      projectEffortInfoDTONew.setProjectNumber(StringUtil.EMPTY);
      projectEffortInfoDTONew.setSeri(StringUtil.EMPTY);
      projectEffortInfoDTONew.setWorkCode(StringUtil.EMPTY);
      projectEffortInfoDTONew.setOldProjectCode(StringUtil.EMPTY);
      projectEffortInfoDTONew.setOldSeri(StringUtil.EMPTY);
      projectEffortInfoDTONew.setOldWorkCode(StringUtil.EMPTY);
      projectEffortInfoDTONew.setWorkName(StringUtil.EMPTY);
      projectEffortInfoDTONew.setActualEffortYearMonth(selectPeriod);
      // updateDisableFields(projectEffortInfoDTONew,disabledDayNameList);
      projectEffortInfoDTOList.add(projectEffortInfoDTONew);
    }
  }

  // Update Project Info with working time in all days of month
  /**
   * Update data actual effort date to effort info.
   * 
   * @param projectEffortInfoDTOList the project effort info dto list
   * @param effortActualParameter the effort actual parameter
   * @param dayOfMonth the day of month
   * @throws AMException the aM exception
   */
  public void updateDataActualEffortDateToEffortInfo(
      List<ProjectEffortInfoDTO> projectEffortInfoDTOList,
      EffortActualParameter effortActualParameter, int dayOfMonth) throws AMException {
    EffortActualDAO effortActualDAO = daoFactory.getEffortActualDAO();
    List<ProjectEffortInfoDTO> projectEffortInfoDTOListWithActualEffortDate =
        new ArrayList<ProjectEffortInfoDTO>();
    try {
      projectEffortInfoDTOListWithActualEffortDate =
          effortActualDAO.getListProjectEffortInfoDTOWithActualEffortDate(effortActualParameter);

      WorkingTimeActualDAO workingTimeActualDAO = daoFactory.getWorkingTimeActualDAO();
      HolidayDAO holidayDAO = daoFactory.getHolidayDAO();
      List<WorkDayDTO> workDayDTOList;
      WorkDayDTO workDayDTOTemp;
      String selectPeriod = effortActualParameter.getSelectPeriod();
      int inputYear = Integer.parseInt(selectPeriod.substring(0, selectPeriod.length() - 2));
      int inputMonth =
          Integer
              .parseInt(selectPeriod.substring(selectPeriod.length() - 2, selectPeriod.length()));
      String dailyWorkingApprovalStatus = StringUtil.EMPTY;
      for (ProjectEffortInfoDTO p1 : projectEffortInfoDTOList) {
        p1.setbDisable(false);
        workDayDTOList =
            createWorkDayDTOList(holidayDAO, selectPeriod, dayOfMonth, inputYear, inputMonth);
        // int i = 0;
        for (ProjectEffortInfoDTO p2 : projectEffortInfoDTOListWithActualEffortDate) {
          if (p1.getProjectCode().equals(p2.getProjectCode())
              && p1.getWorkCode().equals(p2.getWorkCode())
              && p1.getProjectNumber().equals(p2.getProjectNumber())) {
            workDayDTOTemp = workDayDTOList.get(Integer.parseInt(p2.getActualEffortDate()) - 1);
            if (workDayDTOTemp != null) {
              // because of convert Date to String, so format that
              workDayDTOTemp.setWorkTime(p2.getWorkTime().substring(0,
                  p2.getWorkTime().length() - 3));
              workDayDTOTemp.setOldWorkTime(workDayDTOTemp.getWorkTime());
              dailyWorkingApprovalStatus =
                  workingTimeActualDAO.getDailyWorkingActualResultStatus(effortActualParameter
                      .getEmployerCode(), selectPeriod, p2.getActualEffortDate());
              if (!(StringUtil.SPACE_VALUE).equals(dailyWorkingApprovalStatus)
                  && !(StringUtil.EDITING_VALUE).equals(dailyWorkingApprovalStatus)
                  && !(StringUtil.BACK_VALUE).equals(dailyWorkingApprovalStatus)) {
                if (!p1.isbDisable()) {
                  p1.setbDisable(true);
                }
              }
            }
          }
          p1.setWorkDayDTOList(workDayDTOList);
          // i++;
        }// end for p2
      }// end for p1
    } catch (SQLException e) {
      throw new AMSQLException(msgResource.getMessage("error.F0103.error"));
    }
  }

  /**
   * Update disable fields.
   * 
   * @param projectEffortInfo the project effort info
   * @param disabledDayNameList the disabled day name list
   */
  public void updateDisableFields(ProjectEffortInfoDTO projectEffortInfo,
      List<String> disabledDayNameList) {
    for (WorkDayDTO w : projectEffortInfo.getWorkDayDTOList()) {
      for (String dayNumber : disabledDayNameList) {
        if (dayNumber.equals(w.getDayNumber())) {
          w.setbDisable(true);
        }
      }
    }
  }

  /**
   * Sets the holiday ex line list.
   * 
   * @param holidayExLineList the holiday ex line list
   * @param effortActualParameter effort actual parameter
   * @param dayOfMonth the day of month
   */
  public void setHolidayExLineList(List<Boolean> holidayExLineList,
	EffortActualParameter effortActualParameter, int dayOfMonth) throws AMException{
	  holidayExLineList.clear();
	  String selectPeriod = effortActualParameter.getSelectPeriod();
	  HolidayDAO holidayDAO = daoFactory.getHolidayDAO();
	  int inputYear = Integer.parseInt(selectPeriod.substring(0, selectPeriod.length() - 2));
	    int inputMonth =
	        Integer.parseInt(selectPeriod.substring(selectPeriod.length() - 2, selectPeriod.length()));
	    WorkDayDTO workDayDTOTemp;
	    
	    for(int i = 0; i < dayOfMonth; i++){
	    	holidayExLineList.add(false);
	    }
	    try {
	      for (int i = 0; i < dayOfMonth; i++) {
	        workDayDTOTemp = new WorkDayDTO();
	        workDayDTOTemp.setDayName(StringUtil.getDayName(inputYear, inputMonth, i + 1));
	        String day = ((i + 1) < 10) ? "0" + Integer.toString(i + 1) : Integer.toString(i + 1);
	        workDayDTOTemp.setDayNumber(day);
	        // check Holiday
	        if (workDayDTOTemp.getDayName().equals(StringUtil.SUNDAY)
	            || workDayDTOTemp.getDayName().equals(StringUtil.SATURDAY)
	            || holidayDAO.checkHolidaybyDay(selectPeriod, day)) {
	        	holidayExLineList.set(i, true);
	        }
	      }
	    } catch (SQLException e) {
	      throw new AMSQLException(msgResource.getMessage("error.F0103.error"));
	    }
  }

  /**
   * Sets the disable ex line list.
   * 
   * @param disableExLineList the disable ex line list
   * @param projectList the project list
   * @param dayOfMonth the day of month
   */
  public void setDisableExLineList(List<Boolean> disableExLineList,
      List<ProjectEffortInfoDTO> projectList, int dayOfMonth) {
    if (projectList == null || disableExLineList == null) {
      return;
    }
    disableExLineList.clear();
    WorkDayDTO w;

    for (int i = 0; i < dayOfMonth; i++) {
      disableExLineList.add(false);
    }

    for (ProjectEffortInfoDTO p : projectList) {
      for (int i = 0; i < p.getWorkDayDTOList().size(); i++) {
        w = p.getWorkDayDTOList().get(i);
        if (w.isbDisable()) {
          disableExLineList.set(i, true);
        }
      }
    }
  }

  // create WorkdayDTO in all days of input month
  /**
   * Creates the work day dto list.
   * 
   * @param holidayDAO the holiday dao
   * @param selectPeriod the select period
   * @param dayOfMonth the day of month
   * @param inputYear the input year
   * @param inputMonth the input month
   * @return the list
   * @throws AMException the aM exception
   */
  private List<WorkDayDTO> createWorkDayDTOList(HolidayDAO holidayDAO, String selectPeriod,
      int dayOfMonth, int inputYear, int inputMonth) throws AMException {
    List<WorkDayDTO> workDayDTOList = new ArrayList<WorkDayDTO>();
    WorkDayDTO workDayDTOTemp;
    try {
      for (int i = 0; i < dayOfMonth; i++) {
        workDayDTOTemp = new WorkDayDTO();

        workDayDTOTemp.setDayName(StringUtil.getDayName(inputYear, inputMonth, i + 1));
        workDayDTOTemp.setWorkTime(StringUtil.EMPTY);
        workDayDTOTemp.setOldWorkTime(StringUtil.EMPTY);

        String day = ((i + 1) < 10) ? "0" + Integer.toString(i + 1) : Integer.toString(i + 1);
        workDayDTOTemp.setDayNumber(day);
        // check Holiday
        if (workDayDTOTemp.getDayName().equals(StringUtil.SUNDAY)
            || workDayDTOTemp.getDayName().equals(StringUtil.SATURDAY)
            || holidayDAO.checkHolidaybyDay(selectPeriod, day)) {
          workDayDTOTemp.setbHoliday(true);
        }
        workDayDTOList.add(workDayDTOTemp);
      }
    } catch (SQLException e) {
      throw new AMSQLException(msgResource.getMessage("error.F0103.error"));
    }

    return workDayDTOList;
  }

  // set days that field will be disabled by condition daily working actual
  // Result Status
  /**
   * Sets the disable days list.
   * 
   * @param effortActualParameter the effort actual parameter
   * @param dayOfMonth the day of month
   * @return the list
   * @throws AMException the aM exception
   */
  public List<Boolean> setDisableDaysList(EffortActualParameter effortActualParameter,
      int dayOfMonth) throws AMException {
    WorkingTimeActualDAO workingTimeActualDAO = daoFactory.getWorkingTimeActualDAO();
    HolidayDAO holidayDAO = daoFactory.getHolidayDAO();
    List<WorkDayDTO> workDayDTOList;
    String selectPeriod = effortActualParameter.getSelectPeriod();
    int inputYear = Integer.parseInt(selectPeriod.substring(0, selectPeriod.length() - 2));
    int inputMonth =
        Integer.parseInt(selectPeriod.substring(selectPeriod.length() - 2, selectPeriod.length()));
    String dailyWorkingApprovalStatus = StringUtil.EMPTY;
    List<Boolean> disabledDaysList = new ArrayList<Boolean>();
    workDayDTOList =
        createWorkDayDTOList(holidayDAO, selectPeriod, dayOfMonth, inputYear, inputMonth);
    String dayNumberTemp;
    for (int i = 0; i < workDayDTOList.size(); i++) {
      if (i < 10) {
        dayNumberTemp = "0" + Integer.toString(i + 1);
      } else {
        dayNumberTemp = Integer.toString(i + 1);
      }
      try {
        dailyWorkingApprovalStatus =
            workingTimeActualDAO.getDailyWorkingActualResultStatus(effortActualParameter
                .getEmployerCode(), selectPeriod, dayNumberTemp);
        if (!(StringUtil.SPACE_VALUE).equals(dailyWorkingApprovalStatus)
            && !(StringUtil.EDITING_VALUE).equals(dailyWorkingApprovalStatus)
            && !(StringUtil.BACK_VALUE).equals(dailyWorkingApprovalStatus)) {
          disabledDaysList.add(true);
        } else {
          disabledDaysList.add(false);
        }
      } catch (SQLException e) {
        throw new AMSQLException(msgResource.getMessage("error.F0103.error"));
      }
    }
    return disabledDaysList;
  }

}
